package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.service.CustomerService;
import com.atguigu.jxc.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 王先生
 * @Date 2020/12/4 9:07
 * @Version 1.0
 */
@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;
    @Autowired
    private LogService logService;

    @Override
    public Map<String, Object> list(Integer page, Integer rows, String customerName) {
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page-1)*rows;
        List<Customer> customerList = customerDao.getCustomerList(offSet,rows,customerName);
        map.put("rows",customerList);
        map.put("total",customerList.size());
        logService.save(new Log(Log.SELECT_ACTION,"客户列表分页（名称模糊查询）"));
        return map;
    }

    @Override
    public void saveCustomer(Customer customer, Integer customerId) {
        //先判断什么时候是修改，什么时候是保存
        if(customerId!=null){
            customerDao.updateCustomer(customer);
        }else{
            customerDao.saveCustomer(customer);
        }
    }

    @Override
    public void deleteCustomer(String ids) {
        String[] split = ids.split(",");
        for (String id : split) {
            customerDao.deleteCustomer(id);
        }
    }
}
